//
//  HRShowSignResultView.m
//  Hear
//
//  Created by mac on 2018/3/6.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "HRShowSignResultView.h"
#import <POP.h>
#import "UIButton+SSEdgeInsets.h"
@interface HRShowSignResultView ()
@property (nonatomic,strong)UIView *contentView;
@end
@implementation HRShowSignResultView

+(void)showSignResultView:(NSString *)showText andGoldNum:(NSString *)goldNum{
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    HRShowSignResultView * showView = [[self alloc] initWithFrame:[UIScreen mainScreen].bounds];
    showView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    id view = [[window subviews] lastObject];
    if([view isKindOfClass:[HRShowSignResultView class]]){
        
        
        HRShowSignResultView *shareView = (HRShowSignResultView *)view;
        [shareView closeView];
        
    }else{
        [window addSubview:showView];
        
        [showView loadView:showText andGoldNum:goldNum];
    }
}
- (void)loadView:(NSString *)showText andGoldNum:(NSString *)goldNum{
    CGFloat contentWidth = 280;
    CGFloat contentHeight = 300;
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, -contentHeight, contentWidth, contentHeight)];
    [self addSubview:(self.contentView=contentView)];
    contentView.centerX = self.centerX;
    contentView.backgroundColor = [UIColor clearColor];
    
    POPSpringAnimation *anSpring = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionY];
    anSpring.toValue = @(self.centerY);
    anSpring.beginTime           = CACurrentMediaTime()+0.2f;
    anSpring.springBounciness    = 15.0f;
    anSpring.springSpeed         = 12;
    [contentView.layer pop_addAnimation:anSpring forKey:nil];
    
    
    UIImageView *topImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, contentView.width, 110)];
    topImageView.image = [UIImage imageNamed:@"今日任务完成"];
    [contentView addSubview:topImageView];
    
    UIView *superView = [[UIView alloc] initWithFrame:CGRectMake(30, topImageView.bottom, topImageView.width-60, contentHeight-topImageView.bottom-60)];
    superView.backgroundColor = CustomColor(240, 240, 240, 1);
    [contentView addSubview:superView];
    UIBezierPath *maskPath1 = [UIBezierPath bezierPathWithRoundedRect:superView.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(12, 12)];
    CAShapeLayer *maskLayer1 = [[CAShapeLayer alloc] init];
    maskLayer1.frame = superView.bounds;
    maskLayer1.path = maskPath1.CGPath;
    superView.layer.mask = maskLayer1;

    
    
    UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, superView.width-20, superView.height-10)];
    showView.backgroundColor = [UIColor whiteColor];
    [superView addSubview:showView];
    UIBezierPath *maskPath2 = [UIBezierPath bezierPathWithRoundedRect:showView.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(12, 12)];
    CAShapeLayer *maskLayer2 = [[CAShapeLayer alloc] init];
    maskLayer2.frame = showView.bounds;
    maskLayer2.path = maskPath2.CGPath;
    showView.layer.mask = maskLayer2;
    
    
    UILabel *showTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, showView.centerY-20, showView.width, 20)];
    showTextLabel.text = showText;
    showTextLabel.textAlignment = 1;
    showTextLabel.font = CustomUIFont(16);
    showTextLabel.textColor = FirstTextColor;
    [showView addSubview:showTextLabel];
    
    UIButton *showGoldButton = [[UIButton alloc] initWithFrame:CGRectMake(0, showTextLabel.bottom+10, 80, 20)];
    [showGoldButton setImage:[UIImage imageNamed:@"乐币"] forState:0];
    [showGoldButton setTitle:goldNum forState:0];
    [showGoldButton setTitleColor:[UIColor orangeColor] forState:0];
    showGoldButton.titleLabel.font = CustomUIFont(15);
    [showView addSubview:showGoldButton];
    [showGoldButton setImagePositionWithType:SSImagePositionTypeRight spacing:7];
    showGoldButton.centerX = showTextLabel.centerX;
    
    
    UIButton *closeButton = [[UIButton alloc] initWithFrame:CGRectMake((contentView.width-110)/2, superView.bottom+30, 110, 30)];
    [closeButton setTitle:@"点击关闭" forState:0];
    closeButton.titleLabel.font = CustomUIFont(13);
    closeButton.layer.borderColor = [UIColor whiteColor].CGColor;
    closeButton.layer.borderWidth = 1;
    closeButton.layer.cornerRadius = closeButton.height/2;
    [contentView addSubview:closeButton];
    closeButton.alpha = 0;
    [closeButton addTarget:self action:@selector(closeView) forControlEvents:6];
    POPBasicAnimation  *alpha = [POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];
    alpha.toValue             = @(1.f);
    alpha.duration            = 0.7f;
    alpha.beginTime           = CACurrentMediaTime() + 0.8f;
    [closeButton pop_addAnimation:alpha forKey:nil];
    
}

-(void)closeView
{
    [UIView animateWithDuration:0.3f animations:^{
        [self.subviews objectAtIndex:0].top = kFullHeight+500;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];

}



@end
